package com.example.demo.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 数据源配置类
 *
 * @author Frankiegg
 * @date 2018-4-15
 * @version
 */
@Configuration
@ConfigurationProperties( prefix = "spring.druid")
public class DataSourceConfig {

    private String mysqlUrl;
    private String mysqlUsername;
    private String mysqlPassword;
    private String mysqlDriverClassName;

    public void setMysqlUrl(String mysqlUrl) {
        this.mysqlUrl = mysqlUrl;
    }

    public void setMysqlUsername(String mysqlUsername) {
        this.mysqlUsername = mysqlUsername;
    }

    public void setMysqlPassword(String mysqlPassword) {
        this.mysqlPassword = mysqlPassword;
    }

    public void setMysqlDriverClassName(String mysqlDriverClassName) {
        this.mysqlDriverClassName = mysqlDriverClassName;
    }

//    @Primary   //默认数据源
    @Bean(value = "mysqlDB")
    public DruidDataSource mysqlDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(mysqlUrl);
        dataSource.setUsername(mysqlUsername);
        dataSource.setPassword(mysqlPassword);
        dataSource.setDriverClassName(mysqlDriverClassName);
        return dataSource;
    }

//    @Primary
//    @Bean(value = "redisDB")
//    public DataSource oracleDataSource() {
//        return DataSourceBuilder.create().build();
//    }
}
